package com.mlink.ai.chat.ads;

import android.Manifest;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;

import com.adsdk.android.ads.OxAdSdkManager;
import com.adsdk.android.ads.constants.AdEvents;
import com.adsdk.android.ads.constants.CountedEvents;
import com.adsdk.android.ads.constants.OxSdkConstants;
import com.applovin.mediation.MaxAd;
import com.facebook.FacebookSdk;
import com.facebook.appevents.AppEventsConstants;
import com.facebook.appevents.AppEventsLogger;
import com.mlink.ai.chat.utils.EventLoggerKt;

import java.math.BigDecimal;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Currency;
import java.util.List;

public class AdEventUtil {

    /**
     * 上报事件，但需要根据 EventLevel 进行过滤。
     *
     * @param name   事件名称。
     * @param params 事件携带参数。
     */
    public static void trackAdEvent(@NonNull String name, Bundle params) {
        // TA 事件上报，仅仅受 RemoteConfig 的 adsdk_enable_ta_event 控制。
        if (AdEvents.isRequiredEvents(name)) { // 必打点，不受 EventLevel 影响
            doLogEvent(name, params);
            return;
        }
        // 根据设置的事件等级进行决定是否打点。开启 OxSdk DebugMode 时，强制设置为 EventLevel.SDK。
        int eventLevel = OxRemoteConfigHelper.getEventLevel();
        if (eventLevel == EventLevel.NONE) return;
        // 基础打点。
        if (eventLevel >= EventLevel.BASIC) {
            List<String> basicEvents = AdEvents.eventLevelMap.get(EventLevel.BASIC);
            boolean includedInBasicEvents = basicEvents != null && basicEvents.contains(name);
            if (includedInBasicEvents) {
                doLogEvent(name, params);
                return;
            }
        }
        // Sdk 打点。
        if (eventLevel >= EventLevel.SDK) {
            List<String> sdkEvents = AdEvents.eventLevelMap.get(EventLevel.SDK);
            boolean includedInSdkEvents = sdkEvents != null && sdkEvents.contains(name);
            if (includedInSdkEvents) {
                doLogEvent(name, params);
                return;
            }
        }
        // Mediation 打点。
        if (eventLevel >= EventLevel.MEDIATION) {
            List<String> mediationEvents = AdEvents.eventLevelMap.get(EventLevel.MEDIATION);
            boolean includedInMediationEvents = mediationEvents != null && mediationEvents.contains(name);
            if (includedInMediationEvents) {
                doLogEvent(name, params);
            }
        }
    }

    /**
     * 通过 EventLevel 过滤之后，需要进行事件上报。
     *
     * @param name   事件名称。
     * @param params 事件携带参数。
     */
    private static void doLogEvent(@NonNull String name, @Nullable Bundle params) {
        EventLoggerKt.logEvent(name, params);
        if (AdEvents.isFacebookEvent(name)) {
            logFacebookEvent(name, params);
        }
        countEventIfShould(name); // 需要记录部分事件的上报次数。
    }

    /**
     * 向 Firebase Analytics 上报 ad_impression 打点。
     */
    public static void trackFirebaseAdImpressionForMaxAd(@NonNull MaxAd ad) {
        Bundle params = new Bundle();
        params.putString(AdEvents.PARAM_FIREBASE_AD_PLATFORM, "appLovin");
        params.putString(AdEvents.PARAM_FIREBASE_AD_SOURCE, ad.getNetworkName());
        params.putString(AdEvents.PARAM_FIREBASE_AD_FORMAT, ad.getFormat().getDisplayName());
        params.putString(AdEvents.PARAM_FIREBASE_AD_UIT_NAME, ad.getAdUnitId());
        params.putDouble(AdEvents.PARAM_FIREBASE_VALUE, ad.getRevenue());
        params.putString(AdEvents.PARAM_FIREBASE_CURRENCY, "USD"); // All Applovin revenue is sent in USD
        trackAdEvent(AdEvents.EVENT_FIREBASE_AD_IMPRESSION, params);
    }

    /**
     * 确保打点的 value 长度不超过 99。
     */
    private static String ensureSafelyValue(String value) {
        if (value == null || value.length() <= 99) return value;
        return value.substring(0, 99);
    }

    /**
     * 构建基础 Bundle，包含所有点都通用的参数。
     *
     * @param adFormat  广告格式: {@link AdFormat}
     * @param adUnitId  广告 ID。
     * @param placement 场景名称。
     */
    @NonNull
    private static Bundle getBasicBundle(@AdFormat String adFormat, @NonNull String adUnitId, @Nullable String placement) {
        Bundle bundle = new Bundle();
        bundle.putString(AdEvents.PARAM_AD_FORMAT, adFormat);
        bundle.putString(AdEvents.PARAM_AD_UNIT_ID, adUnitId);
        if (placement != null) {
            bundle.putString(AdEvents.PARAM_PLACEMENT, placement);
        }
        Context context = OxAdSdkManager.getInstance().getContext();
        if (context != null) {
            bundle.putString(AdEvents.PARAM_VERSION, getVersionName(context));
            // TODO 能否监听网络状态变化。
            int networkType = getNetworkTypeWithVpnState(context);
            int networkState = getNetworkState(context);
            bundle.putInt(AdEvents.PARAM_NETWORK_TYPE, networkType);
            bundle.putInt(AdEvents.PARAM_NETWORK_STATE, networkState);
        }
        return bundle;
    }

    /**
     * 请求广告。
     */
    public static void trackAdRequestEvent(@AdFormat String adFormat, @NonNull String adUnitId, @Nullable String placement) {
        Bundle bundle = getBasicBundle(adFormat, adUnitId, placement);
        trackAdEvent(AdEvents.EVENT_AD_REQUEST, bundle);
    }

    /**
     * 广告加载完成。
     */
    public static void trackAdLoadedEvent(@AdFormat String adFormat, @NonNull String adUnitId, @Nullable String placement,
                                          long loadedDuration, @Nullable String bannerSize, @Nullable String networkName,
                                          @Nullable String waterfallName, int networkResponses, long latencyMillis,
                                          @Nullable String creativeId, double revenue) {
        Bundle bundle = getBasicBundle(adFormat, adUnitId, placement);
        bundle.putLong(AdEvents.PARAM_LOADED_DURATION, loadedDuration);
        if (bannerSize != null) {
            bundle.putString(AdEvents.PARAM_SIZE, bannerSize);
        }
        if (networkName != null) {
            bundle.putString(AdEvents.PARAM_NETWORK_NAME, networkName);
        }
        if (waterfallName != null) {
            bundle.putString(AdEvents.PARAM_WATERFALL_NAME, waterfallName);
        }
        if (networkResponses >= 0) {
            bundle.putInt(AdEvents.PARAM_NETWORK_RESPONSES, networkResponses);
        }
        if (latencyMillis >= 0) {
            bundle.putLong(AdEvents.PARAM_LATENCY_MILLIS, latencyMillis);
        }
        if (creativeId != null) {
            bundle.putString(AdEvents.PARAM_CREATIVE_ID, creativeId);
        }
        bundle.putDouble(AdEvents.PARAM_REVENUE, revenue);
        trackAdEvent(AdEvents.EVENT_AD_LOADED, bundle);
    }

    /**
     * 广告加载失败。
     */
    public static void trackAdLoadFailedEvent(@AdFormat String adFormat, @NonNull String adUnitId, @Nullable String placement,
                                              @Nullable String error, long failedDuration) {
        Bundle bundle = getBasicBundle(adFormat, adUnitId, placement);
        if (error != null) {
            bundle.putString(AdEvents.PARAM_ERROR, ensureSafelyValue(error));
        }
        bundle.putLong(AdEvents.PARAM_FAILED_DURATION, failedDuration);
        trackAdEvent(AdEvents.EVENT_AD_FAILED, bundle);
    }

    /**
     * 客户端调用 showAd 系列方法。
     */
    public static void trackAdShowEvent(@AdFormat String adFormat, @NonNull String adUnitId, @Nullable String placement, @Nullable String limitation) {
        Bundle bundle = getBasicBundle(adFormat, adUnitId, placement);
        if (limitation != null) {
            bundle.putString(AdEvents.PARAM_LIMITATION, limitation);
        }
        trackAdEvent(AdEvents.EVENT_AD_SHOW, bundle);
    }

    /**
     * 客户端调用 showAd 后，Sdk 进行必要检查（比如 isReady（）），检查通过后调用 Mediation 的广告展示 API。
     */
    public static void trackAdShowingEvent(@AdFormat String adFormat, @NonNull String adUnitId, @Nullable String placement,
                                           @Nullable String networkName, @Nullable String creativeId) {
        Bundle bundle = getBasicBundle(adFormat, adUnitId, placement);
        if (networkName != null) {
            bundle.putString(AdEvents.PARAM_NETWORK_NAME, networkName);
        }
        if (creativeId != null) {
            bundle.putString(AdEvents.PARAM_CREATIVE_ID, creativeId);
        }
        trackAdEvent(AdEvents.EVENT_AD_SHOWING, bundle);
    }

    /**
     * 广告展示成功（Displayed 回调）。
     */
    public static void trackAdImpressionEvent(@AdFormat String adFormat, @NonNull String adUnitId, @Nullable String placement,
                                              @Nullable String bannerSize, @Nullable String networkName, @Nullable String waterfallName,
                                              int networkResponses, long latencyMillis,
                                              @Nullable String creativeId, double revenue) {
        Bundle bundle = getBasicBundle(adFormat, adUnitId, placement);
        if (bannerSize != null) {
            bundle.putString(AdEvents.PARAM_SIZE, bannerSize);
        }
        if (networkName != null) {
            bundle.putString(AdEvents.PARAM_NETWORK_NAME, networkName);
        }
        if (waterfallName != null) {
            bundle.putString(AdEvents.PARAM_WATERFALL_NAME, waterfallName);
        }
        if (networkResponses >= 0) {
            bundle.putInt(AdEvents.PARAM_NETWORK_RESPONSES, networkResponses);
        }
        if (latencyMillis >= 0) {
            bundle.putLong(AdEvents.PARAM_LATENCY_MILLIS, latencyMillis);
        }
        if (creativeId != null) {
            bundle.putString(AdEvents.PARAM_CREATIVE_ID, creativeId);
        }
        bundle.putDouble(AdEvents.PARAM_REVENUE, revenue);
        trackAdEvent(AdEvents.EVENT_AD_IMPRESSION, bundle);
    }

    /**
     * 广告展示失败（DisplayFailed 回调）。
     */
    public static void trackAdShowFailedEvent(@AdFormat String adFormat, @NonNull String adUnitId, @Nullable String placement,
                                              @Nullable String bannerSize, @Nullable String networkName, @Nullable String waterfallName,
                                              int networkResponses, @Nullable String creativeId, @Nullable String error, long failedDuration) {
        Bundle bundle = getBasicBundle(adFormat, adUnitId, placement);
        if (bannerSize != null) {
            bundle.putString(AdEvents.PARAM_SIZE, bannerSize);
        }
        if (networkName != null) {
            bundle.putString(AdEvents.PARAM_NETWORK_NAME, networkName);
        }
        if (waterfallName != null) {
            bundle.putString(AdEvents.PARAM_WATERFALL_NAME, waterfallName);
        }
        if (networkResponses >= 0) {
            bundle.putInt(AdEvents.PARAM_NETWORK_RESPONSES, networkResponses);
        }
        if (creativeId != null) {
            bundle.putString(AdEvents.PARAM_CREATIVE_ID, creativeId);
        }
        if (error != null) {
            bundle.putString(AdEvents.PARAM_ERROR, ensureSafelyValue(error));
        }
        bundle.putLong(AdEvents.PARAM_FAILED_DURATION, failedDuration);
        trackAdEvent(AdEvents.EVENT_AD_SHOW_FAILED, bundle);
    }

    /**
     * 广告被点击。
     */
    public static void trackAdClickEvent(@AdFormat String adFormat, @NonNull String adUnitId, @Nullable String placement,
                                         @Nullable String bannerSize, @Nullable String networkName, @Nullable String waterfallName,
                                         int networkResponses, @Nullable String creativeId, double revenue) {
        Bundle bundle = getBasicBundle(adFormat, adUnitId, placement);
        if (bannerSize != null) {
            bundle.putString(AdEvents.PARAM_SIZE, bannerSize);
        }
        if (networkName != null) {
            bundle.putString(AdEvents.PARAM_NETWORK_NAME, networkName);
        }
        if (waterfallName != null) {
            bundle.putString(AdEvents.PARAM_WATERFALL_NAME, waterfallName);
        }
        if (networkResponses >= 0) {
            bundle.putInt(AdEvents.PARAM_NETWORK_RESPONSES, networkResponses);
        }
        if (creativeId != null) {
            bundle.putString(AdEvents.PARAM_CREATIVE_ID, creativeId);
        }
        bundle.putDouble(AdEvents.PARAM_REVENUE, revenue);
        trackAdEvent(AdEvents.EVENT_AD_CLICK, bundle);
    }

    /**
     * 广告被关闭。
     */
    public static void trackAdCloseEvent(@AdFormat String adFormat, @NonNull String adUnitId, @Nullable String placement,
                                         @Nullable String bannerSize, @Nullable String networkName, @Nullable String waterfallName,
                                         int networkResponses, long latencyMillis,
                                         @Nullable String creativeId, double revenue, long showingDuration) {
        Bundle bundle = getBasicBundle(adFormat, adUnitId, placement);
        if (bannerSize != null) {
            bundle.putString(AdEvents.PARAM_SIZE, bannerSize);
        }
        if (networkName != null) {
            bundle.putString(AdEvents.PARAM_NETWORK_NAME, networkName);
        }
        if (waterfallName != null) {
            bundle.putString(AdEvents.PARAM_WATERFALL_NAME, waterfallName);
        }
        if (networkResponses >= 0) {
            bundle.putInt(AdEvents.PARAM_NETWORK_RESPONSES, networkResponses);
        }
        if (latencyMillis >= 0) {
            bundle.putLong(AdEvents.PARAM_LATENCY_MILLIS, latencyMillis);
        }
        if (creativeId != null) {
            bundle.putString(AdEvents.PARAM_CREATIVE_ID, creativeId);
        }
        bundle.putDouble(AdEvents.PARAM_REVENUE, revenue);
        bundle.putDouble(AdEvents.PARAM_SHOWING_DURATION, showingDuration);
        trackAdEvent(AdEvents.EVENT_AD_CLOSE, bundle);
    }

    /**
     * 获得 RV 奖励。
     */
    public static void trackAdGottenCredit(@AdFormat String adFormat, @NonNull String adUnitId, @Nullable String placement,
                                           @Nullable String bannerSize, @Nullable String networkName, @Nullable String waterfallName,
                                           int networkResponses, @Nullable String creativeId, double revenue) {
        Bundle bundle = getBasicBundle(adFormat, adUnitId, placement);
        if (bannerSize != null) {
            bundle.putString(AdEvents.PARAM_SIZE, bannerSize);
        }
        if (networkName != null) {
            bundle.putString(AdEvents.PARAM_NETWORK_NAME, networkName);
        }
        if (waterfallName != null) {
            bundle.putString(AdEvents.PARAM_WATERFALL_NAME, waterfallName);
        }
        if (networkResponses >= 0) {
            bundle.putInt(AdEvents.PARAM_NETWORK_RESPONSES, networkResponses);
        }
        if (creativeId != null) {
            bundle.putString(AdEvents.PARAM_CREATIVE_ID, creativeId);
        }
        bundle.putDouble(AdEvents.PARAM_REVENUE, revenue);
        trackAdEvent(AdEvents.EVENT_AD_GOTTEN_CREDIT, bundle);
    }

    public static final String LIMITATION_AD_NOT_READY = OxSdkConstants.AdShowLimitation.AD_NOT_READY;
    public static final String LIMITATION_AD_CONTAINER_NOT_FOUND = OxSdkConstants.AdShowLimitation.AD_CONTAINER_NOT_FOUND;
    public static final String LIMITATION_NETWORK_UNAVAILABLE = OxSdkConstants.AdShowLimitation.NETWORK_UNAVAILABLE;
    public static final String LIMITATION_MEMORY_LIMIT_REACHED = "Memory limit reached";
    public static final String LIMITATION_AD_VIEW_BINDER_NOT_SET = "ViewBinder not set";

    /**
     * 获取 网络类型 + Vpn 状态。
     */
    private static int getNetworkTypeWithVpnState(Context context) {
        int vpnState = getVpnState(context);
        int networkType = getNetworkType(context);
        return vpnState + networkType;
    }

    /**
     * 获取网络类型。
     */
    @NetworkType
    private static int getNetworkType(Context context) {
        if (getNetworkState(context) == NetworkState.UNAVAILABLE)
            return NetworkType.UNKNOWN; // 网络未连接时。
        if (isWifiConnected(context)) return NetworkType.WIFI; // WIFI 连接。
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            // 非 WIFI 连接，但因缺少权限而不能进一步划分数据网络类型。
            return NetworkType.DATA;
        }
        /* 不用担心这个 error，当不存在这个权限时，无法执行以下逻辑。
         这样做的目的，READ_PHONE_STATE 是个比较敏感的危险权限，不希望在 SDK 中添加。
         但有可能客户端使用并申请了该权限，这时，可以利用这个权限，获取移动网络类型。
         */
        TelephonyManager mTelephonyManager = (TelephonyManager)
                context.getSystemService(Context.TELEPHONY_SERVICE);
        int networkType = mTelephonyManager.getNetworkType();
        switch (networkType) {
            case TelephonyManager.NETWORK_TYPE_GPRS:
            case TelephonyManager.NETWORK_TYPE_EDGE:
            case TelephonyManager.NETWORK_TYPE_CDMA:
            case TelephonyManager.NETWORK_TYPE_1xRTT:
            case TelephonyManager.NETWORK_TYPE_IDEN:
                return NetworkType.DATA_2G;
            case TelephonyManager.NETWORK_TYPE_UMTS:
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
            case TelephonyManager.NETWORK_TYPE_HSDPA:
            case TelephonyManager.NETWORK_TYPE_HSUPA:
            case TelephonyManager.NETWORK_TYPE_HSPA:
            case TelephonyManager.NETWORK_TYPE_EVDO_B:
            case TelephonyManager.NETWORK_TYPE_EHRPD:
            case TelephonyManager.NETWORK_TYPE_HSPAP:
                return NetworkType.DATA_3G;
            case TelephonyManager.NETWORK_TYPE_LTE:
                return NetworkType.DATA_4G;
            case TelephonyManager.NETWORK_TYPE_NR:
                return NetworkType.DATA_5G;
            default:
                return NetworkType.DATA;
        }
    }

    /**
     * 判断 Wifi 是否连接。
     */
    private static boolean isWifiConnected(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        return netInfo != null && netInfo.isConnected() && netInfo.getType() == ConnectivityManager.TYPE_WIFI;
    }

    /**
     * 获取网络连接状态。
     */
    @NetworkState
    private static int getNetworkState(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        boolean isNetworkAvailable = netInfo != null && netInfo.isConnected();
        return isNetworkAvailable ? NetworkState.AVAILABLE : NetworkState.UNAVAILABLE;
    }

    /**
     * 判断是否使用 VPN。
     */
    private static boolean isVpnActive(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Network activeNetwork = cm.getActiveNetwork();
            NetworkCapabilities caps = cm.getNetworkCapabilities(activeNetwork);
            if (caps != null) {
                return caps.hasTransport(NetworkCapabilities.TRANSPORT_VPN);
            }
        }
        List<String> networkList = new ArrayList<>();
        try {
            for (NetworkInterface networkInterface : Collections.list(NetworkInterface.getNetworkInterfaces())) {
                if (networkInterface.isUp())
                    networkList.add(networkInterface.getName());
            }
        } catch (Exception ignored) {
        }
        return networkList.contains("tun0");
    }

    @VpnState
    private static int getVpnState(Context context) {
        return isVpnActive(context) ? VpnState.CONNECTED : VpnState.DISCONNECTED;
    }

    /**
     * 获取客户端 VersionName。
     */
    @Nullable
    private static String getVersionName(Context context) {
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    context.getPackageName(), 0);
            return packageInfo.versionName;

        } catch (PackageManager.NameNotFoundException ignored) {
        }
        return null;
    }

    private static final String PREF_NAME_EVENTS_FREQUENCY = "OxSDK_events_frequency";
    private static final String FREQUENCY_PREFIX = "frequency_of";

    /**
     * 记录上报目标事件的次数。
     *
     * @param name 事件名称。
     */
    private static void countEventIfShould(@NonNull String name) {
        if (!AdEvents.eventsShouldBeCounted.contains(name)) return;
        Context context = OxAdSdkManager.getInstance().getContext();
        if (context == null) return;
        SharedPreferences sp = context.getSharedPreferences(PREF_NAME_EVENTS_FREQUENCY, Context.MODE_PRIVATE);
        String key = FREQUENCY_PREFIX + name;
        int frequency = sp.getInt(key, 0);
        if (frequency == Integer.MAX_VALUE) frequency = 0;
        sp.edit().putInt(key, frequency + 1).apply();
    }

    /**
     * 获取 Sdk 上报目标事件的次数。
     */
    public static int getFrequencyOfEvent(@CountedEvents int event) {
        String name = AdEvents.getCountEventName(event);
        if (TextUtils.isEmpty(name) || !AdEvents.eventsShouldBeCounted.contains(name)) {
            return -1;
        }
        Context context = OxAdSdkManager.getInstance().getContext();
        if (context == null) {
            return -1;
        }
        SharedPreferences sp = context.getSharedPreferences(PREF_NAME_EVENTS_FREQUENCY, Context.MODE_PRIVATE);
        String key = FREQUENCY_PREFIX + name;
        return sp.getInt(key, 0);
    }

    public static void logFacebookEvent(@NonNull String eventName, @Nullable Bundle params) {
        if (!FacebookSdk.isInitialized()) {
            return;
        }
        Context context = OxAdSdkManager.getInstance().getContext();
        if (context == null) return;
        AppEventsLogger logger = AppEventsLogger.newLogger(context);
        // 需要对 Ad_Impression_Revenue 打点进行特殊处理。
        if (AdEvents.EVENT_AD_IMPRESSION_REVENUE.equals(eventName)) {
            if (params == null) return;
            // 设置 currency 参数。
            String currency = params.getString(OxAdValueUtils.PARAM_KEY_CURRENCY, "USD");
            params.putString(AppEventsConstants.EVENT_PARAM_CURRENCY, currency);
            // 设置 content type 参数。
            params.putString(AppEventsConstants.EVENT_PARAM_CONTENT_TYPE, "product");
            // 设置 content id 参数。
            String adUnitId = "";
            String platform = params.getString(OxAdValueUtils.PARAM_KEY_AD_PLATFORM);
            if (OxAdValueUtils.AD_PLATFORM_ADMOB.equals(platform)) {
                adUnitId = params.getString(OxAdValueUtils.PARAM_KEY_PLACEMENT_ID);
            } else if (OxAdValueUtils.AD_PLATFORM_MAX.equals(platform)) {
                adUnitId = params.getString(OxAdValueUtils.PARAM_KEY_AD_UNIT_IDENTIFIER);
            }
            params.putString(AppEventsConstants.EVENT_PARAM_CONTENT_ID, adUnitId);
            // 获取 revenue
            double revenue = params.getDouble(OxAdValueUtils.PARAM_KEY_REVENUE, 0.0);
            logger.logPurchase(BigDecimal.valueOf(revenue), Currency.getInstance("USD"), params);
        } else {
            logger.logEvent(eventName, params);
        }
    }
}
